<?php

namespace App\Http\Controllers\unicall;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SysFunction extends Controller
{
    /**1
     * 主叫路由添加
     * parammap中key为主叫号码value为被叫号码 ** php 版本用数组处理同理
     * @param appver:应用版本值
     * @param user:用户名
     * @param pwd
     * @param account:企业400号码
     */

    private $expiredtime = false;
    private $token = "";
    private $config;


    function __construct()
    {
        $this->config = new SysConfig();
    }

    function AddCallRouting($appver, $user, $pwd, $account, $parammap)
    {
        try {


            $token = $this->login($appver, $user, $pwd);


            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {

                $tokenmap = array();
                $paramlist = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                if (!is_array($parammap))
                    throw new Exception("你输入的参数不是数组类型");

                $i = 0;
                foreach ($parammap as $key => $value) {
                    $paramlist[$i]["callingid"] = $key;
                    $paramlist[$i]["calledid"] = $value;
                    $i++;
                }

                $sttr = json_encode($paramlist);
                $tokenmap["data"] = $sttr;

                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;

                $url = $this->config->getAddCallRoutingURL();//getaddCallRoutingURL;

                $res = $this->post($tokenmap, $url);
                return $res;

            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            echo $res;
        }

    }


    /**2
     * 主叫路由清空
     */
    function ClearCallRouting($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            //获取token
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();
                $paramlist = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $url = $this->config->getClearCallRoutingURL();//ConstantModel.clearCallRoutingURL;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }


    /**3
     * 主叫路由删除
     * @param callingid:主叫号码，多个号码英文逗号分隔，上限500个。
     */
    function DeleteCallRouting($appver, $user, $pwd, $account, $callingid)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $tokenmap["callingid"] = $callingid;
                $url = $this->config->getdeleteCallRoutingURL();

                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;

                $res = $this->post($tokenmap, $url);
                return $res;

            }
            return $res;

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**4
     * 主叫路由查询
     * @param callingid:主叫号码，多个号码英文逗号分隔，上限500个。
     */
    function QueryCallRouting($appver, $user, $pwd, $account, $callingid)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $tokenmap["callingid"] = $callingid;
                $url = $this->config->getQueryCallRoutingURL();

                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;

                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**5
     * 主叫路由数量查询
     */
    function QueryCallRoutingSize($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryCallRoutingSizeURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**6
     * 虚拟分机添加
     * @param Map key存放dn分机编号， value存放tel目的码
     */
    function AddExtension($appver, $user, $pwd, $account, $parammap)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                if (!is_array($parammap))
                    throw new Exception("你输入的参数不是数组类型");

                $i = 0;
                foreach ($parammap as $key => $value) {
                    $paramlist[$i]["tel"] = $value;
                    $paramlist[$i]["dn"] = $key;
                    $i++;
                }

                $sttr = json_encode($paramlist);
                $tokenmap["data"] = $sttr;


                $url = $this->config->getAddExtensionURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**7
     * 虚拟分机批量查询
     * @param pageSize:一页的数量
     * @param pageNow:页编号
     */
    function BatchQueryExtension($appver, $user, $pwd, $account, $pageSize, $pageNow)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["pageNow"] = $pageNow;
                $tokenmap["pageSize"] = $pageSize;
                /*

                                      if(!is_array($parammap))
                                          throw new Exception("你输入的参数不是数组类型");

                                      $i = 0;
                                      foreach ($parammap as $key=>$value)
                                      {
                                          $paramlist[$i]["tel"] = $value;
                                          $paramlist[$i]["dn"] = $key;
                                          $i++;
                                      }

                                      $sttr = json_encode($paramlist);
                                      $tokenmap["data"] = $sttr;*/


                $url = $this->config->getBatchQueryExtensionURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**8
     * 虚拟分机清空
     */
    function ClearExtension($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getClearExtensionURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**9
     * 虚拟分机删除
     * @param dn 目的码
     */
    function DeleteExtension($appver, $user, $pwd, $account, $dn)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["dn"] = $dn;

                $url = $this->config->getDeleteExtensionURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**10
     * 虚拟分机查询
     * @param dn 目的吗
     */
    function QueryExtension($appver, $user, $pwd, $account, $dn)
    {
        try {


            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["dn"] = $dn;

                $url = $this->config->getQueryExtensionURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**11
     * 隐号添加
     * @param Map key存放phone， value存放phone
     */
    function AddHiddedPhone($appver, $user, $pwd, $account, $parammap)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                if (!is_array($parammap))
                    throw new Exception("你输入的参数不是数组类型");

                $i = 0;
                foreach ($parammap as $key => $value) {
                    $paramlist[$i]["phone2"] = $value;
                    $paramlist[$i]["phone1"] = $key;
                    $i++;
                }

                $sttr = json_encode($paramlist);
                $tokenmap["data"] = $sttr;


                $url = $this->config->getAddHiddedPhonegURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**12
     * 隐号清空
     */
    function ClearHiddedPhone($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getClearHiddedPhoneURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**13
     * 隐号删除
     * @param phone1:号码，多个号码则英文逗号分隔，上限500个。
     */
    function DeleteHiddedPhone($appver, $user, $pwd, $account, $phone1)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["phone1"] = $phone1;

                $url = $this->config->getDeleteHiddedPhoneURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**14
     * 隐号查询
     * @param phone1:号码，多个号码则英文逗号分隔，上限500个。
     */
    function QueryHiddedPhone($appver, $user, $pwd, $account, $phone1)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["phone1"] = $phone1;

                $url = $this->config->getQueryHiddedPhoneURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**15
     * 隐号数量查询
     */
    function QueryHiddedPhoneSize($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryHiddedPhoneSizeURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**16
     * 黑名单添加
     * @param phone 号码，多个号码则英文逗号分隔，上限500个
     */
    function AddBlackList($appver, $user, $pwd, $account, $phone)
    {
        try {


            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["phone"] = $phone;

                $url = $this->config->getAddBlackListURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**17
     * 黑名单清空
     */
    function ClearBlackList($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getClearBlackListURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**18
     *黑名单删除
     * @param phone 号码，多个号码则英文逗号分隔，上限500个
     */
    function DeleteBlackList($appver, $user, $pwd, $account, $phone)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["phone"] = $phone;

                $url = $this->config->getDeleteBlackListURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**19
     * 黑名单查询
     */
    function QueryBlackList($appver, $user, $pwd, $account)
    {
        try {


            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryBlackListURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**20
     *动态目标码删除
     * @param transferid:转接节点ID
     */
    function DeleteDynaDestNum($appver, $user, $pwd, $account, $transferid)
    {
        try {


            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["transferid"] = $transferid;

                $url = $this->config->getDeleteDynaDestNumURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**21
     *动态目标码查询
     * @param transferid:转接节点ID
     */
    function QueryDynaDestNum($appver, $user, $pwd, $account, $transferid)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["transferid"] = $transferid;

                $url = $this->config->getQueryDynaDestNumURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**22
     *动态目标码更新
     * @param transferid:转接节点ID
     * @param destNum:目标号码，多个号码英文逗号分隔，例如：134104456433,13433233443
     */
    function UpdateDynaDestNum($appver, $user, $pwd, $account, $transferid, $destNum)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["transferid"] = $transferid;
                $tokenmap["destNum"] = $destNum;

                $url = $this->config->getUpdateDynaDestNumURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**23
     * 呼叫事件提取
     */
    function QueryCallEvent($appver, $user, $pwd, $account)
    {
        try {


            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryCallEventURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**24
     * 点击呼叫结果查询
     */
    function QueryClickCall($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryClickCallURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }

        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**25
     * 点击呼叫申请
     * callingid:主叫号码
     * calledid:被叫号码
     */
    function RequestClickCall($appver, $user, $pwd, $account, $callingid, $calledid)
    {
        try {

//            $res = "";
//            $token = $this->login($appver, $user, $pwd);
//
//            $result = "用户名或者密码错误";
//            if ("" == $token || "1" == $token || $token == null) {
//                return $result;
//
//            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["callingid"] = $callingid;
                $tokenmap["calledid"] = $calledid;

                $url = $this->config->getRequestClickCallURL();
                $secret = $this->signTopRequest($tokenmap, $pwd);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
//            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**26
     * 通话记录查询
     * @param startTime:通话记录创建的开始时间，格式：yyyyMMddHHmmss，开始和结束时间必须是同一月份，不支持跨月查询。
     * @param endTime:通话记录创建的结束时间，格式：yyyyMMddHHmmss，开始和结束时间必须是同一月份，不支持跨月查询。
     * @param pageSize:一页的通话数量，最大500
     * @param pageNow:页编号，从1开始
     */
    function QueryCallRecord($appver, $user, $token, $account, $startTime, $endTime, $pageSize, $pageNow)
    {
        try {

            $tokenmap = array();

            $time4 = date("YmdHis");
            $tokenmap["timestamp"] = $time4;
            $tokenmap["appver"] = $appver;
            $tokenmap["user"] = $user;
            $tokenmap["account"] = $account;
            $tokenmap["startTime"] = $startTime;
            $tokenmap["endTime"] = $endTime;
            $tokenmap["account"] = $account;
            $tokenmap["pageNow"] = $pageNow;
            $tokenmap["pageSize"] = $pageSize;

            $url = $this->config->getQueryCallRecordURL();
            $secret = $this->signTopRequest($tokenmap, $token);
            $tokenmap["secret"] = $secret;
            $res = $this->post($tokenmap, $url);
            return $res;
        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }


    /**27
     * 满意度记录查询
     * @param startTime:通话记录创建的开始时间，格式：yyyyMMddHHmmss，开始和结束时间必须是同一月份，不支持跨月查询。
     * @param endTime:通话记录创建的结束时间，格式：yyyyMMddHHmmss，开始和结束时间必须是同一月份，不支持跨月查询。
     * @param pageSize:一页的通话数量，最大500
     * @param pageNow:页编号，从1开始
     */
    function QuerySatisfaction($appver, $user, $pwd, $account, $startTime, $endTime, $pageSize, $pageNow)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["startTime"] = $startTime;
                $tokenmap["endTime"] = $endTime;
                $tokenmap["account"] = $account;
                $tokenmap["pageNow"] = $pageNow;

                $url = $this->config->getQueryCallRecordURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }


    /**28
     *    短信报告查询
     * @param serviceSn 短信流水号，如果不填，则返回有运营商报告的短信记录，上限500条。
     */
    function QuerySmsReport($appver, $user, $pwd, $account, $serviceSn)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["serviceSn"] = $serviceSn;

                $url = $this->config->getQuerySmsReportURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**29
     *    短信发送
     * @param List: 发送的短信内容，JSONArray格式，数量和模板中变量个数匹配。示例：短信模板为：您的验证码为{1},客服电话{2}
     *                data：["123567","13810443533"]平台会自动和模板匹配，生成短信：您的验证码123567,客服电话13810443533。
     * @param:    mobile接收短信的手机号码多个手机号码，英文逗号分隔，上限100个。
     * @param:templateId模板编号客户提供短信模板到信通工作人员，工作人员在管理平台审核通过后，提供给客户该编号。
     */
    function RequestSmsSend($appver, $user, $pwd, $account, $paramlist, $mobile, $templateId)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["account"] = $account;
                $tokenmap["templateId"] = $templateId;
                $tokenmap["mobile"] = $mobile;

                if (!is_array($paramlist))
                    throw new Exception("你输入的参数不是数组类型");

                $sttr = json_encode($paramlist);
                $tokenmap["data"] = $sttr;

                $url = $this->config->getRequestSmsSendURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**30
     * 网页呼叫结果提取
     */
    function QueryWebcall($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryWebcallURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /**31
     * 网页呼叫申请
     * @param callingid:主叫号码，手机号码或者固话号码，手机号码格式:18611334885, 不需要添加0或者区号。固话格式：01053358846，不需要添加任何其它非数字内容。
     */
    function RequestWebcall($appver, $user, $pwd, $account, $callingid)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["callingid"] = $callingid;

                $url = $this->config->getRequestWebcallURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }


    /**32
     * 语音验证码结果查询
     */
    function VoiceResultSearch($appver, $user, $pwd, $account)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;

                $url = $this->config->getQueryVoiceURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }

    /** 33
     * 语音验证码申请
     * @param dnis:被叫号码
     * @param voicecode:语音验证码
     */
    function voice($appver, $user, $pwd, $account, $dnis, $voicecode)
    {
        try {

            $res = "";
            $token = $this->login($appver, $user, $pwd);

            $result = "用户名或者密码错误";
            if ("" == $token || "1" == $token || $token == null) {
                return $result;

            } else {
                $tokenmap = array();

                $time4 = date("YmdHis");
                $tokenmap["timestamp"] = $time4;
                $tokenmap["appver"] = $appver;
                $tokenmap["user"] = $user;
                $tokenmap["account"] = $account;
                $tokenmap["dnis"] = $dnis;
                $tokenmap["voicecode"] = $voicecode;

                $url = $this->config->getVoiceURL();
                $secret = $this->signTopRequest($tokenmap, $token);
                $tokenmap["secret"] = $secret;
                $res = $this->post($tokenmap, $url);
                return $res;
            }


        } catch (Exception $e) {

            $res = "异常信息如下：" . $e->getMessage();
            return $res;

        }

    }


    function login($appver, $user, $pwd)
    {
        try {

            $token = $this->loginmethod($appver, $user, $pwd);
            return $token;

        } catch (Exception $e) {

        }

    }


    function loginmethod($appver, $user, $pwd)
    {

        try {

            $mappram = $this->union400Login($appver, $user, $pwd);

            $url = $this->config->getLoginURL();

            $res = $this->post($mappram, $url);

            $json = json_decode($res);


            //取出token和expiredtime


            $errcode = $json->errcode;

            if ("1" == ($errcode)) {
                return $errcode;

            } else {
                $jsonqdate = $json->data;

                $this->expiredtime = $jsonqdate->expiredtime;
                $token = $jsonqdate->token;
                return $token;
            }

        } catch (Exception $e) {

        }
        return null;

    }


    function post($post_string, $remote_server)
    {
        try {

            $ch = curl_init();
            // var_dump($remote_server);
            curl_setopt($ch, CURLOPT_URL, $remote_server);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_string));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, "");
            $data = curl_exec($ch);
            curl_close($ch);

            return $data;

        } catch (Exception $e) {
            return "";
        }
        return "";
    }


    function union400Login($appver, $user, $pwd)
    {
        //接收并处理传递参数
        $map = array();
        $map["appver"] = $appver;
        $map["user"] = $user;

        //获取当前时间
        $endtime = date("YmdHis");

        //$endtime = "20170503183759";
        //计算Secrect
        $map["timestamp"] = $endtime;
        //var_dump($endtime);
        $map["secret"] = $this->signTopRequest($map, $pwd);

        return $map;
    }

    function signTopRequest($map, $pwd)
    {
        //1 删除secret
        //params.remove("secret");

        //2检查参数是否已经排序
        // String[] keys = params.keySet().toArray(new String[0]);
        // Arrays.sort(keys);

        //3把所有参数名和参数值串在一起
        ksort($map);
        $query = http_build_query($map);
        $query = str_replace("&", "", $query);
        $query = str_replace("=", "", $query);
        //4使用MD5加密
        $query .= $pwd;

        // $query= "appver=1&user=4000371141_dev&timestamp=2017050317531626552s6s5y5y5q5955555sqe262y0295";


        $bytes = MD5($query);


        //  var_dump($query);
        //5转换为字符串
        return strtoupper($bytes);
        // return "54D9ED54998500CB57C3CD3FAAE0DFE9";


    }
}
